package de.tum.in.eist.im.client.Login;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.in.eist.im.client.Registration.RegistrationPanel;

public class LoginPanel {
	
	// private variables
	final private VerticalPanel loginPanel = new VerticalPanel();
	final private TextBox usernameBox = new TextBox();
	final private PasswordTextBox passwordBox = new PasswordTextBox();
	final private Label errorLabel = new Label();
	final private Button loginButton = new Button();
	final private Button registrationButton = new Button();
	
	/**
	 * default constructor creates a login panel
	 */
	public LoginPanel() {
		// get the root panel
		RootLayoutPanel rootPanel = RootLayoutPanel.get();
		rootPanel.clear();
		
		// create username TextBox
		usernameBox.setText("Username:");
		usernameBox.setFocus(true);
		usernameBox.selectAll();
		
		// create password TextBox
		passwordBox.setText("Password:");
		
		// create login Button
		loginButton.setText("Login");
		
		// create registration button
		registrationButton.setText("Registration");
		
		// create horizontal panel for the login and registration button
		HorizontalPanel buttonPanel = new HorizontalPanel();
		buttonPanel.add(loginButton);
		buttonPanel.add(registrationButton);
		
		// create vertical panel
		loginPanel.add(usernameBox);
		loginPanel.add(passwordBox);		
		loginPanel.add(buttonPanel);
		loginPanel.add(errorLabel);
		
		// add the login panel to the root panel
		rootPanel.add(loginPanel);
		
		// add styles
		loginPanel.addStyleName("centerPanel");
		errorLabel.addStyleName("errorLabel");
		
		// add handler for the login process
		LoginHandler loginHandler = new LoginHandler(this);
		usernameBox.addKeyUpHandler(loginHandler);
		passwordBox.addKeyUpHandler(loginHandler);
		loginButton.addClickHandler(loginHandler);
		
		// add handler for the registration process
		registrationButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				new RegistrationPanel();
			}	
		});
	}
	
	/**
	 * get the username
	 * @return username
	 */
	public String getUsername() {
		return this.usernameBox.getText();
	}
	
	/**
	 * get the password
	 * @return the password
	 */
	public String getPassword() {
		return this.passwordBox.getText();
	}
	
	/**
	 * set the error label
	 * @param error the error label
	 */
	public void setErrorLabel(String error) {
		this.errorLabel.setText(error);
	}
}
